package com.sgb.kjwl.view.ui.fragment.pick;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.motion.widget.MotionLayout;
import androidx.core.app.ActivityCompat;
import androidx.databinding.DataBindingUtil;

import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.apeng.permissions.EsayPermissions;
import com.apeng.permissions.OnPermission;
import com.chuanglan.shanyan_sdk.OneKeyLoginManager;
import com.chuanglan.shanyan_sdk.listener.OneKeyLoginListener;
import com.chuanglan.shanyan_sdk.listener.OpenLoginAuthListener;
import com.google.android.material.tabs.TabLayout;
import com.sgb.alimaplibrary.MarkerEntity;
import com.sgb.alimaplibrary.SelectAddressActivity;
import com.sgb.alimaplibrary.widget.MapMarkerView;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.GsonManager;
import com.sgb.kjwl.databinding.FragmentPickMapBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.BiddingEntity;
import com.sgb.kjwl.model.entity.LoginEntity;
import com.sgb.kjwl.model.entity.TenderListEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.pick.TenderAllListEntity;
import com.sgb.kjwl.model.entity.user.OneKeyLoginEntity;
import com.sgb.kjwl.utils.ConfigUtils;
import com.sgb.kjwl.utils.TextViewUtils;
import com.sgb.kjwl.utils.TouchHelper;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LoginEvent;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickNoticeDetailActivity;
import com.sgb.kjwl.view.ui.activity.pick.TenderDetailsActivity3;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.sgb.kjwl.view.widget.PickHallBidingPopWindow;
import com.sgb.kjwl.view.widget.pick.PickMapBiddingPopWindow;
import com.sgb.kjwl.view.widget.pick.PickMapPopAllWindow;
import com.sgb.kjwl.view.widget.pick.PickMapPopWindow;
import com.sgb.kjwl.view.widget.pick.PickPopWindowView;
import com.sgb.kjwl.viewmodel.pick.PickMapFragmentModel;
import com.sgb.link.utils.PickCateType;
import com.sgb.zhaocai.widget.threefliter.ZCThreeLevelFilterWindow;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.mmkvutil.CacheMarker;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.util.PickSelectTextEntity;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.selection.CommonPopEntity;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;

/**
 * 招标-地图
 */
public class PickMapFragment extends AppLazyFragment {

    private FragmentPickMapBinding binding;
    @Inject
    PickMapFragmentModel pickMapFragmentModel;

    private PickHallBidingPopWindow pickDistancePopWindow;
    private PickHallBidingPopWindow pickTabLayoutPopWindow;
    private PickHallBidingPopWindow pickBidTypePopWindow;

    private List<PickSelectEntity> typeDatas = new ArrayList<>();
    private List<CommonPopEntity> commonlist;
    private List<CommonPopEntity> childrenCommonlist;
    private List<PickSelectEntity> tabTypeList = new ArrayList<>();
    private List<PickSelectEntity> bidTypeData = new ArrayList<>();
    private PickSelectEntity all;

    //类型
    private String typeNos = "";

    private String goodsType = "";
    private String goodsTypeName;
    private String distance = "1000"; //距离筛选字段
    private String distanceName;
    private String bidTypeName;
    private String bidTypeNo = "0";

    private int tabPosition = -1;
    private int tabTypePosition;
    private boolean isLoadMore = false;
    private Double latitude; //纬度
    private Double longitude; //经度
    List<MarkerEntity> markerEntityList = new ArrayList<>();
    private PickMapPopAllWindow pickMapPopAllWindow;
    private PickMapPopWindow pickMapPopWindow;
    private PickMapBiddingPopWindow pickMapBiddingPopWindow;
    private List<TenderAllListEntity> tenderAllListEntityList = new ArrayList<>();
    private List<BiddingEntity> biddingListEntityMapList = new ArrayList<>();
    private List<TenderListEntity> tenderListEntityMapList = new ArrayList<>();
    //是否是展开的筛选栏，默认展开
    private boolean mIsExpand = true;
    //手势监听帮助类
    private TouchHelper mTouchHelper;
    private ZCThreeLevelFilterWindow popCategory;
    private String defaultAddress;
    private LatLng latLngLocation;
    private boolean isShowPopWindow = false;
    private boolean isSearch = false;
    private boolean isMarkerClick = true;

    public static PickMapFragment newInstance() {
        PickMapFragment fragment = new PickMapFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent
                .builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_pick_map, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        binding.mMapView.initMap(savedInstanceState, getLifecycle(), 30, new MapMarkerView.CallBack() {

            @Override
            public void initMap() {
                super.initMap();
                if ((ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                        ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) || !isLocationEnabled()) {
                    //当用户未开启定位功能时，需要用户使用首页定位的城市位置作为筛选条件，故再地图初始化完成时，调用时
                    if (latitude != null && longitude != null)
                        binding.mMapView.getLatLotExtra(latitude, longitude);
                }

            }

            @Override
            public void onMarkerClick(int pos, String title) {

            }

            @Override
            public void onLocation(Double lat, Double Log, String string) {
                latitude = lat;
                longitude = Log;
                latLngLocation = new LatLng(Double.valueOf(latitude), Double.valueOf(longitude));
                defaultAddress = string;
                setSearchContent(string);
                binding.mMapView.mapMarkerTools.setMyLocation(latitude, longitude, R.drawable.pick_location);
            }

            @Override
            public void onMarkerClick(int pos, String title, Marker marker) {
                super.onMarkerClick(pos, title, marker);
                isShowPopWindow = true;
                isMarkerClick = false;
                showMapPopWindow(pos);
            }

            @Override
            public void onRegeocodeSearched(Double Lat, Double Log, String string) {
                super.onRegeocodeSearched(Lat, Log, string);
                if (!TextUtils.isEmpty(string)) {
                    defaultAddress = string;
                    setSearchContent(string);
                    binding.mMapView.zoomToSpan(latLngLocation);
                }
            }
        });
    }

    /**
     * 地图和列表模式切换
     */
    private void setMapOrList() {
        if (!isLocationEnabled()) {
            if (!TextUtils.isEmpty((pickMapFragmentModel.getLocation())[1]) && !TextUtils.isEmpty((pickMapFragmentModel.getLocation())[0])) {
                longitude = Double.valueOf((pickMapFragmentModel.getLocation())[1]);
                latitude = Double.valueOf((pickMapFragmentModel.getLocation())[0]);
                latLngLocation = new LatLng(Double.valueOf(latitude), Double.valueOf(longitude));
                if (binding.mMapView.mapMarkerTools == null) {
                    return;
                }
                if (latitude != null && longitude != null) {
                    binding.mMapView.getLatLotExtra(latitude, longitude);
                }
            }
            return;
        }
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            if (!TextUtils.isEmpty((pickMapFragmentModel.getLocation())[1]) && !TextUtils.isEmpty((pickMapFragmentModel.getLocation())[0])) {
                longitude = Double.valueOf((pickMapFragmentModel.getLocation())[1]);
                latitude = Double.valueOf((pickMapFragmentModel.getLocation())[0]);
                latLngLocation = new LatLng(Double.valueOf(latitude), Double.valueOf(longitude));
                if (binding.mMapView.mapMarkerTools == null) {
                    return;
                }
                if (latitude != null && longitude != null) {
                    binding.mMapView.getLatLotExtra(latitude, longitude);
                }
            }
            return;
        }
        setSearchContent(SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.PROVINCE_ID, ""));
    }

    private void setSearchContent(String name) {
        binding.etSearch.setText(name);
        binding.etSearch.setTextColor(getResources().getColor(R.color.color_333333));
        binding.tvSearchDelete.setVisibility(View.VISIBLE);
        binding.searchImage.setBackgroundResource(R.mipmap.ditu_gray);
    }

    private void init() {

        //距离
        pickDistancePopWindow = new PickHallBidingPopWindow(getActivity());
        pickDistancePopWindow.setBgView(binding.grayLayout);
        pickDistancePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                updateChooseStatus(PickCateType.TYPE_DISTANCE, (Integer) key3);
                initFilterData(bidTypeNo);
            }
        });
        pickDistancePopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                int value = getItemPos(PickCateType.TYPE_DISTANCE);
                updateChooseTextStatus(binding.tvDistance, value > 0 ? 1 : 0, distanceName);
            }
        });

        //招标形式
        pickBidTypePopWindow = new PickHallBidingPopWindow(getActivity());
        pickBidTypePopWindow.setBgView(binding.grayLayout);
        pickBidTypePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                bidTypeName = ((PickSelectEntity) key1).getProjectscale();
                bidTypeNo = ((PickSelectEntity) key1).getNo();
                if("全部".equals(bidTypeName)){
                    bidTypeName = "招标形式";
                }
                updateChooseTextStatus(binding.tvBidType, (Integer) key3, bidTypeName);
                initFilterData(bidTypeNo);
            }
        });
        pickBidTypePopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                if(TextUtils.isEmpty(bidTypeNo)){
                    bidTypeNo = "0";
                }
                updateChooseTextStatus(binding.tvBidType, Integer.valueOf(bidTypeNo) == 0 ? 0 : 1, bidTypeName);
            }
        });

        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tabPosition = tab.getPosition();
                typeNos = typeDatas.get(tabPosition).getNo();
                pickMapFragmentModel.tenderWayByType(typeNos);
                isLoadMore = false;
                if (tabPosition == 0 || tabPosition == (typeDatas.size() - 1)) {
                    binding.tvType.setVisibility(View.GONE);
                    if (mData != null && mData.size() > 0 && mData.get(0) != null) {
                        if (mData.get(0).getTypeName().contains("类别")) {
                            mData.remove(0);
                        }
                    }
                    initFilterData(bidTypeNo);
                } else {
                    binding.tvType.setVisibility(View.VISIBLE);
                    resetCategoryFilterPopWindow();
                }
                if (tabPosition == 0 || tabPosition == (typeDatas.size() - 1)) {
                    if (tabPosition == (typeDatas.size() - 1)) {
                        binding.tvType.setVisibility(View.GONE);
                    }
                    binding.lineTabType.setVisibility(View.GONE);
                } else {
                    tabTypeList.clear();
                    binding.tabLayoutType.setTabRippleColor(ColorStateList.valueOf(getContext().getResources().getColor(R.color.translucent)));
                    binding.tabLayoutType.removeAllTabs();
                    binding.lineTabType.setVisibility(View.VISIBLE);
                    binding.tvType.setVisibility(View.GONE);
                    mFilters.put(PickCateType.TYPE_CATEGORY_0, 0);
                    mFilters.put(PickCateType.TYPE_CATEGORY_1, 0);
                    mFilters.put(PickCateType.TYPE_CATEGORY_2, 0);
                }

                if (tab != null && tab.getCustomView() != null) {
                    TextView tabSelect = tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
                    tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    tabSelect.setTextColor(getResources().getColor(R.color.white));
                    tabSelect.setTextSize(16);
                    tabSelect.invalidate();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab != null && tab.getCustomView() != null) {
                    TextView tabSelect = tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
                    tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    tabSelect.setTextColor(getResources().getColor(R.color.white));
                    tabSelect.setTextSize(15);
                    tabSelect.invalidate();
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        binding.tabLayoutType.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                isLoadMore = false;
                tabTypePosition = tab.getPosition();
                if (tabTypePosition == 0) {
                    binding.tvType.setVisibility(View.GONE);
                } else {
                    childrenCommonlist = commonlist.get(tabTypePosition).getChildren();
                    if (childrenCommonlist == null || childrenCommonlist.size() == 0) {
                        binding.tvType.setVisibility(View.GONE);
                    } else {
                        goodsType = "";
                        goodsTypeName = "";
                        updateChooseTextStatus(binding.tvType, TextUtils.isEmpty(goodsType) ? 0 : 1, goodsTypeName);
                        binding.tvType.setVisibility(View.VISIBLE);
                        popCategory.setShowThreeRecycler(false);
                        popCategory.setDismiss(false);
                        popCategory.setData(childrenCommonlist, 0);
                    }
                }
                pickTabLayoutPopWindow.setDefItem(tabTypePosition);
                goodsType = tabTypeList.get(tabTypePosition).getNo();
                initFilterData(bidTypeNo);

                if (tab != null && tab.getCustomView() != null) {
                    TextView tabSelect = tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
                    tabSelect.setTextSize(14);
                    tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    tabSelect.setTextColor(getResources().getColor(R.color.APPTabColor));
                    tabSelect.invalidate();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab != null && tab.getCustomView() != null) {
                    TextView tabSelect = tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
                    tabSelect.setTextSize(14);
                    tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    tabSelect.setTextColor(getResources().getColor(R.color.color_666666));
                    tabSelect.invalidate();
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        //类别二级
        pickTabLayoutPopWindow = new PickHallBidingPopWindow(getActivity());
        pickTabLayoutPopWindow.setBgView(binding.grayLayout);
        pickTabLayoutPopWindow.setOnResultClick((key1, key2, key3) -> {
            binding.tabLayoutType.getTabAt((int) key3).select();
            pickTabLayoutPopWindow.setDefItem((int) key3);
            goodsType = ((PickSelectEntity) key1).getNo();
            initFilterData(bidTypeNo);
        });
        pickTabLayoutPopWindow.setOnDisMissClick(() -> {
            binding.surveyView.setImageResource(R.drawable.btn_down);
        });

        all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");

        pickMapPopAllWindow = new PickMapPopAllWindow(getActivity(), 3, new PickMapPopAllWindow.CallBack() {
            @Override
            public void onMoreData(boolean more) {
                Log.e("api:", "倒数第二个 onMoreData");
                isLoadMore = true;
                initFilterData(bidTypeNo);
            }

            @Override
            public void onItemClickAdapter(TenderAllListEntity tenderAllListEntity) {
                if (tenderAllListEntity.getBusinessType() == 2) { //1.招标； 2.竞价
                    pickMapFragmentModel.getUrlSetting(tenderAllListEntity.getId());
                } else if (tenderAllListEntity.getBusinessType() == 1) {
                    if (tenderAllListEntity.getState() == 8) {
                        PickNoticeDetailActivity.start(getActivity(), tenderAllListEntity.getId() + "");
                    } else {
                        TenderDetailsActivity3.start(getActivity(), tenderAllListEntity.getId() + "", 0);
                    }
                } else if (tenderAllListEntity.getBusinessType() == 3) {
                    pickMapFragmentModel.getWebH5(tenderAllListEntity.getId(), tenderAllListEntity.getModuleType());
                }
            }

            @Override
            public void onDismiss() {
                isShowPopWindow = false;
                setMarkerBig(null);
            }

            @Override
            public void onShowPosition(int position) {
                showPositionMap(position);
            }

            @Override
            public void onReport(TenderAllListEntity tenderAllListEntity) {
                pickMapFragmentModel.countByBusiness(tenderAllListEntity.getId(), tenderAllListEntity.getBusinessType());
            }

            @Override
            public void onContactService(TenderAllListEntity tenderAllListEntity) {
                if ("8".equals(tenderAllListEntity.getState())) {
                    MToast.showToast(getContext(), "招标预告状态不可以发起交流");
                    return;
                }
                if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    jumpLogin();
                } else {
                    if (!TextUtils.isEmpty(tenderAllListEntity.getCustomerServiceContactsId())) {
                        pickMapFragmentModel.findCustomerServiceImAccount(tenderAllListEntity.getCustomerServiceContactsId());
                    } else {
                        checkAllowDialog("招标方未设置客服人员，不支持交流。");
                    }
                }
            }

            @Override
            public void onInquireByLetter(TenderAllListEntity tenderAllListEntity) {
                if ("8".equals(tenderAllListEntity.getState())) {
                    MToast.showToast(getContext(), "招标预告状态不可以发起函询");
                    return;
                }
                if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    jumpLogin();
                } else {
                    if (tenderAllListEntity.getBusinessType() == 1) {
                        pickMapFragmentModel.checkAllowTenderQuestion(tenderAllListEntity.getId(), tenderAllListEntity.getType());
                    } else if (tenderAllListEntity.getBusinessType() == 2) {
                        pickMapFragmentModel.checkAllowBidQuestion(tenderAllListEntity.getId(), tenderAllListEntity.getType());
                    }
                }
            }

            @Override
            public void onLineUserFocus(TenderAllListEntity tenderAllListEntity, int position) {
                if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    jumpLogin();
                } else {
                    if (tenderAllListEntity.getBusinessType() == 1) {
                        pickMapFragmentModel.editTender(tenderAllListEntity.getId(), position);
                    } else {
                        pickMapFragmentModel.editBid(tenderAllListEntity.getId(), position);
                    }
                }
            }
        });

        pickMapPopWindow = new PickMapPopWindow(getActivity(), 3, new PickMapPopWindow.CallBack() {
            @Override
            public void onMoreData(boolean more) {
                pickMapFragmentModel.findTenderMapPage(typeNos, "", "", goodsType, latitude, longitude, distance, "", true, "", "", "");
            }

            @Override
            public void onDismiss() {
                isShowPopWindow = false;
                setMarkerBig(null);
            }

            @Override
            public void onShowPosition(int position) {
                showPositionMap(position);
            }

            @Override
            public void onItemClick(TenderListEntity tenderListEntity) {
                TenderDetailsActivity3.start(getActivity(), tenderListEntity.getId() + "", 0);
            }

            @Override
            public void onReport(TenderListEntity tenderListEntity) {
                pickMapFragmentModel.countByBusiness(tenderListEntity.getId(), 1);
            }

            @Override
            public void onContactService(TenderListEntity tenderListEntity) {
                if ("8".equals(tenderListEntity.getStatus())) {
                    MToast.showToast(getContext(), "招标预告状态不可以发起交流");
                    return;
                }
                if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    jumpLogin();
                } else {
                    // 跳转H5聊天
                    if (!TextUtils.isEmpty(tenderListEntity.getCustomerServiceContactsId())) {
                        pickMapFragmentModel.findCustomerServiceImAccount(tenderListEntity.getCustomerServiceContactsId());
                    } else {
                        checkAllowDialog("招标方未设置客服人员，不支持交流。");
                    }
                }
            }

            @Override
            public void onInquireByLetter(TenderListEntity tenderListEntity) {
                if ("8".equals(tenderListEntity.getStatus())) {
                    MToast.showToast(getContext(), "招标预告状态不可以发起函询");
                    return;
                }
                if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    jumpLogin();
                } else {
                    // 验证当前用户是否允许函询--招标
                    pickMapFragmentModel.checkAllowTenderQuestion(tenderListEntity.getId(), tenderListEntity.getVerticalTypeValue());
                }
            }

            @Override
            public void onLineUserFocus(String tenderNo, int position) {
                if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    jumpLogin();
                } else {
                    pickMapFragmentModel.editTender(tenderNo, position);
                }
            }
        });

        //地图详情-竞价
        pickMapBiddingPopWindow = new PickMapBiddingPopWindow(getActivity(), 3, new PickMapBiddingPopWindow.CallBack() {
            @Override
            public void onMoreData(boolean more) {
                Log.e("api:", "倒数第二个 onMoreData");
                pickMapFragmentModel.getBiddingMapList(distance, typeNos, "", "", true, "", goodsType, latitude, longitude);
            }

            @Override
            public void onItemClickAdapter(String id) {
                pickMapFragmentModel.getUrlSetting(id);
            }

            @Override
            public void onDismiss() {
                isShowPopWindow = false;
                setMarkerBig(null);
            }

            @Override
            public void onShowPosition(int position) {
                showPositionMap(position);
            }

            @Override
            public void onReport(BiddingEntity biddingEntity) {
                pickMapFragmentModel.countByBusiness(biddingEntity.getId(), 1);
            }

            @Override
            public void onContactService(BiddingEntity biddingEntity) {
                if ("8".equals(biddingEntity.getState())) {
                    MToast.showToast(getContext(), "招标预告状态不可以发起交流");
                    return;
                }
                if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    jumpLogin();
                } else {
                    if (!TextUtils.isEmpty(biddingEntity.getCustomerServiceContactsId())) {
                        pickMapFragmentModel.findCustomerServiceImAccount(biddingEntity.getCustomerServiceContactsId());
                    } else {
                        checkAllowDialog("招标方未设置客服人员，不支持交流。");
                    }
                }
            }

            @Override
            public void onInquireByLetter(BiddingEntity biddingEntity) {
                if ("8".equals(biddingEntity.getState())) {
                    MToast.showToast(getContext(), "招标预告状态不可以发起函询");
                    return;
                }
                if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    jumpLogin();
                } else {
                    pickMapFragmentModel.checkAllowBidQuestion(biddingEntity.getId(), biddingEntity.getType());
                }
            }

            @Override
            public void onLineUserFocus(BiddingEntity biddingEntity, int position) {
                if ("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    jumpLogin();
                } else {
                    pickMapFragmentModel.editBid(biddingEntity.getId(), position);
                }
            }

        });

        binding.mMapView.getMap().setOnMapTouchListener(motionEvent -> {
            if (mTouchHelper == null) {
                mTouchHelper = TouchHelper.newInstance().addTouchListener(new TouchHelper.TouchListenerProxy() {
                    @Override
                    public void handlerUpTouch(float downX, float downY, float upX, float upY) {
                        //纵向滑动距离小于-100px
                        if (mIsExpand && upY - downY < -100) {
                            binding.mlFilterRoot.transitionToEnd();
                        }
                    }
                });
            }
            mTouchHelper.onTouch(null, motionEvent);
        });
        binding.mlFilterRoot.setTransitionListener(new MotionLayout.TransitionListener() {
            @Override
            public void onTransitionStarted(MotionLayout motionLayout, int i, int i1) {

            }

            @Override
            public void onTransitionChange(MotionLayout motionLayout, int i, int i1, float v) {

            }

            @Override
            public void onTransitionCompleted(MotionLayout motionLayout, int i) {
                mIsExpand = binding.mlFilterRoot.getProgress() == 0.0f;
                if (mIsExpand) {
                    binding.tvExpand.setText("收起筛选条件");
                    TextViewUtils.changeTextView(getActivity(), binding.tvExpand, R.drawable.btn_shop_up, false);
                } else {
                    binding.tvExpand.setText("展开筛选条件");
                    TextViewUtils.changeTextView(getActivity(), binding.tvExpand, R.drawable.btn_shop_down, false);
                }
            }

            @Override
            public void onTransitionTrigger(MotionLayout motionLayout, int i, boolean b, float v) {
            }
        });
        initCateGoryPopup();
    }

    private void initCateGoryPopup() {
        popCategory = new ZCThreeLevelFilterWindow(getContext());
        popCategory.setBgView(binding.grayLayout);
        popCategory.setOnResultClick(new ZCThreeLevelFilterWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                CommonPopEntity pickResultProject1 = (CommonPopEntity) key1;
                CommonPopEntity pickResultProject2 = (CommonPopEntity) key2;
                CommonPopEntity pickResultProject3 = (CommonPopEntity) key3;
                if (!"全部".equals(pickResultProject1.getTypeName())) {
                    goodsTypeName = pickResultProject1.getTypeName();
                    goodsType = pickResultProject1.getNo();
                } else {
                    goodsTypeName = "";
                    goodsType = "";
                    popCategory.setData(childrenCommonlist, 0);
                }
                if (pickResultProject2 != null && !TextUtils.isEmpty(pickResultProject2.getTypeName())) {
                    if (!"全部".equals(pickResultProject1.getTypeName())) {
                        goodsTypeName = pickResultProject1.getTypeName();
                        goodsType = pickResultProject1.getNo();
                    } else {
                        goodsTypeName = "";
                        goodsType = "";
                    }
                }
                if (pickResultProject3 != null && !TextUtils.isEmpty(pickResultProject3.getTypeName())) {
                    if (!"全部".equals(pickResultProject1.getTypeName())) {
                        goodsTypeName = pickResultProject1.getTypeName();
                        goodsType = pickResultProject1.getNo();
                    } else {
                        goodsTypeName = "";
                        goodsType = "";
                    }
                }
                updateChooseTextStatus(binding.tvType, TextUtils.isEmpty(goodsType) ? 0 : 1, goodsTypeName);
                //刷新数据
                initFilterData(bidTypeNo);
            }

            @Override
            public void reset() {
                goodsType = "";
                goodsTypeName = "";
                popCategory.setData(childrenCommonlist, 0);
                updateChooseTextStatus(binding.tvType, TextUtils.isEmpty(goodsType) ? 0 : 1, goodsTypeName);
                //刷新数据
                initFilterData(bidTypeNo);
            }
        });
        popCategory.setOnDismissListener(() -> {
            updateChooseTextStatus(binding.tvType, TextUtils.isEmpty(goodsType) ? 0 : 1, goodsTypeName);
        });
    }

    public void checkAllowDialog(String msg) {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);

        tv_logout_cancel.setVisibility(View.GONE);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("提示");

        tv_message.setText(msg);

        //确定
        tv_logout_confirm.setOnClickListener(v -> roundCornerDialog.dismiss());
    }

    public void setTvIsGz(int position, String data) {
        MToast.showToast(getContext(), data);
        switch (bidTypeNo) {
            case "0":
                pickMapPopAllWindow.updateFocus(position);
                break;
            case "1":
                pickMapPopWindow.updateFocus(position);
                break;
            case "2":
                pickMapBiddingPopWindow.updateFocus(position);
                break;
        }
    }

    private void initFilterData(String no) {
        if (latitude == null || longitude == null) {
            return;
        }
        switch (no) {
            case "0":
                pickMapFragmentModel.findTenderingHomeEsPageMap(typeNos, goodsType, "", "", "", isLoadMore, "", distance, latitude, longitude);
                break;
            case "1":
                pickMapFragmentModel.findTenderMapPage(typeNos, "", "", goodsType, latitude, longitude, distance, "", false, "", "", "");
                break;
            case "2":
                pickMapFragmentModel.getBiddingMapList(distance, typeNos, "", "", false, "", goodsType, latitude, longitude);
                break;
        }
    }

    private void initViewData() {
        binding.tvType.setOnClickListener(this);
        binding.surveyView.setOnClickListener(this);
        binding.tvBidType.setOnClickListener(this);
        binding.tvDistance.setOnClickListener(this);
        binding.ivBack.setOnClickListener(this);
        binding.tvSearchDelete.setOnClickListener(this);
        binding.llChangeAddress.setOnClickListener(this);
        binding.llSearch.setOnClickListener(this);
        binding.etSearch.setOnClickListener(this);
        //距离筛选条件
        String[] strings = new String[]{"10", "20", "50", "100", "200", "500"};
        setDistanceDatas(strings);
    }

    public void getUrl(String url, String id) {
        WebViewX5HostActivity.start(getActivity(), url, id);
    }

    @Override
    protected void onLazyLoad() {
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        initViewData();
        init();
    }

    private void initData() {
        pickMapFragmentModel.getFilterTypeData();
        initFilterData(bidTypeNo);
        setMapOrList();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_type:
                if (TextUtils.isEmpty(goodsTypeName)) {
                    goodsTypeName = "类别";
                }
                updateChooseTextStatus(binding.tvType, TextUtils.isEmpty(goodsType) ? -1 : -2, goodsTypeName);
                new Handler().postDelayed(() -> popCategory.showPopwindow0(binding.tvType), 200);
                break;
            case R.id.tv_bid_type:
                if(bidTypeData == null || bidTypeData.size() == 0) return;
                if (TextUtils.isEmpty(bidTypeName)) {
                    bidTypeName = "招标形式";
                }
                updateChooseTextStatus(binding.tvBidType, "0".equals(bidTypeNo) ? -1 : -2, bidTypeName);
                pickBidTypePopWindow.showPopwindow0(binding.tvBidType);
                pickBidTypePopWindow.setData(bidTypeData, false);
                break;
            case R.id.survey_view:
                if (tabTypeList != null && tabTypeList.size() != 0){
                    binding.surveyView.setImageResource(R.drawable.btn_shop_up);
                    pickTabLayoutPopWindow.showPopwindow(binding.surveyView);
                    pickTabLayoutPopWindow.setData(tabTypeList, binding.tabLayoutType.getSelectedTabPosition());
                }
                break;
            case R.id.tv_distance:
                if (TextUtils.isEmpty(distanceName)) {
                    distanceName = "距离";
                }
                updateChooseTextStatus(binding.tvDistance, "1000".equals(distance) ? -1 : -2, distanceName);
                pickDistancePopWindow.showPopwindow0(binding.tvDistance);
                int value = getItemPos(PickCateType.TYPE_DISTANCE);
                pickDistancePopWindow.setData(getItemData(PickCateType.TYPE_DISTANCE), value);
                break;
            case R.id.iv_back:
                getActivity().finish();
                break;
            case R.id.ll_change_address:
            case R.id.ll_search:
            case R.id.et_search:
                jumpSelectAddress();
                break;
            case R.id.tv_search_delete:
                binding.searchImage.setBackgroundResource(R.drawable.ic_search_lease_map);
                binding.etSearch.setText("请输入地址");
                binding.etSearch.setTextColor(getResources().getColor(R.color.color_c0c4cc));
                binding.tvSearchDelete.setVisibility(View.GONE);
                break;

        }
    }

    private void jumpSelectAddress() {
        //手动定位--不能获取位置信息时
        Intent intent = new Intent();
        intent.setClass(getActivity(), SelectAddressActivity.class);
        intent.putExtra(SelectAddressActivity.TITLE, "我的位置");
        intent.putExtra(SelectAddressActivity.SAVE_TEXT, "使用该定位地址");
        intent.putExtra(SelectAddressActivity.SHOWLOCATION, "SHOWLOCATION");
        intent.putExtra(SelectAddressActivity.LATITUDE, latitude);
        intent.putExtra(SelectAddressActivity.LONGTITUDE, longitude);
        startActivityForResult(intent, 255);

        //待写，关闭弹框
    }

    private void setDistanceDatas(String[] strings) {
        PickSelectEntity all = new PickSelectEntity();
        all.setNo("1000");
        all.setProjectscale("全部");
        all.setCheck(true);
        List<PickSelectEntity> distanceDates = new ArrayList<>();
        distanceDates.add(0, all);
        for (String s : strings) {
            PickSelectEntity pickSelectEntity = new PickSelectEntity();
            pickSelectEntity.setNo(s);
            pickSelectEntity.setProjectscale(s + "km以内");
            distanceDates.add(pickSelectEntity);
        }
        refreshDataInfo(PickCateType.TYPE_DISTANCE, "距离", distanceDates);

    }

    public void setBidTypeData(List<PickSelectEntity> datas) {
        bidTypeData.clear();
        bidTypeData.addAll(datas);
    }

    private void resetCategoryFilterPopWindow() {
        getCategoryAllList();
    }

    private void getCategoryAllList() {
        pickMapFragmentModel.getTenderGoodsCategoryAllList(typeNos);
    }


    /**
     * 添加数据到中数据聚合
     */
    private void refreshDataInfo(int sort, String name, List<PickSelectEntity> children) {
        addEntity(sort, children, name);
    }

    // 数据集
    private SparseArray<PickSelectTextEntity> mData = new SparseArray<>();
    private SparseIntArray mFilters = new SparseIntArray();
    private SparseArray<String> nameArray = new SparseArray<>();

    public void addEntity(int index, List<PickSelectEntity> list) {
        if (mData.get(index) != null) {
            if (list == null || list.size() == 0) {
                mData.get(index).setPickSelectEntityList(new ArrayList<>());
            } else {
                mData.get(index).setPickSelectEntityList(list);
            }
        } else {
            mData.put(index, new PickSelectTextEntity(index, nameArray.get(index), list));
        }
        mFilters.put(index, 0);
    }

    public void addEntity(int index, List<PickSelectEntity> list, String name) {
        if (mData.get(index) != null) {
            if (list == null || list.size() == 0) {
                mData.get(index).setPickSelectEntityList(new ArrayList<>());
            } else {
                mData.get(index).setPickSelectEntityList(list);
            }
            mData.get(index).setTypeName(name);
        } else {
            mData.put(index, new PickSelectTextEntity(index, name, list));
        }
        mFilters.put(index, 0);
    }

    /**
     * 获取当前缓存位置下标
     *
     * @param pos index
     */
    public int getItemPos(int pos) {
        return mFilters.get(pos);
    }

    public List<PickSelectEntity> getItemData(int index) {
        if (mData.get(index) != null) {
            return mData.get(index).getPickSelectEntityList();
        }
        return new ArrayList<>();
    }

    /**
     * 筛选赋值
     */
    private void updateChooseStatus(int key, int value) {
        mFilters.put(key, value);
        String no = mData.get(key).getItemNo(value);
        String name = mData.get(key).getItemName(value);
        switch (key) {
            case PickCateType.TYPE_DISTANCE: // 距离
                distanceName = name;
                if ("距离".equals(name)) {
                    no = "1000";
                }
                distance = no;
                if (value > 0) {
                    updateChooseTextStatus(binding.tvDistance, value, name);
                }
                break;
        }
    }

    /**
     * 更新选项颜色
     */
    private void updateChooseTextStatus(PickPopWindowView pickPopWindowView, int value, String name) {
        if (!TextUtils.isEmpty(name)) {
            if (name.length() > 4) {
                name = name.substring(0, 4) + "...";
            }
        }
        if (value == -1 || value == -2) {
            binding.popLine.setVisibility(View.VISIBLE);
            pickPopWindowView.setChooseData(R.drawable.member_pop_blue_bg2, R.mipmap.ic_up_arrow_n, R.color.color_333333, true, name);
        } else if (value == 0) {
            binding.popLine.setVisibility(View.GONE);
            pickPopWindowView.setChooseData(R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333, false, name);
        } else {
            binding.popLine.setVisibility(View.GONE);
            pickPopWindowView.setChooseData(R.drawable.search_new_blue_bg_10, R.mipmap.icon_down_red, R.color.color_main, false, name);
        }
    }

    /**
     * 分类
     */
    public void setTenderGoodsCategoryAllList(List<CommonPopEntity> data) {
        this.commonlist = data;

        for (int i = 0; i < commonlist.size(); i++) {
            PickSelectEntity pickSelectEntity = new PickSelectEntity(commonlist.get(i).getNo(), commonlist.get(i).getTypeName());
            tabTypeList.add(pickSelectEntity);
            binding.tabLayoutType.addTab(binding.tabLayoutType.newTab().setText(pickSelectEntity.getProjectscale()));
            binding.tabLayoutType.getTabAt(i).setCustomView(R.layout.tab_item_textview);

            TextView tv = binding.tabLayoutType.getTabAt(i).getCustomView().findViewById(R.id.tv_tab_item_textview);
            tv.setTextSize(14);
            tv.setTextColor(getResources().getColor(R.color.color_666666));
            if (pickSelectEntity.getProjectscale().length() > 4) {
                tv.setText(pickSelectEntity.getProjectscale().substring(0, 4) + "...");
            } else {
                if (i == 0) {
                    tv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    if (isAdded()) {
                        tv.setTextColor(getResources().getColor(R.color.APPTabColor));
                    }
                }
                tv.setText(pickSelectEntity.getProjectscale());
            }
        }
    }

    public void setTypeDatas(List<PickSelectEntity> datas) {
        if (datas == null) return;
        typeDatas.clear();
        typeDatas.addAll(datas);

        for (int i = 0; i < typeDatas.size(); i++) {
            binding.tabLayout.addTab(binding.tabLayout.newTab(), false);
            binding.tabLayout.getTabAt(i).setCustomView(R.layout.tab_item_textview);

            View view = binding.tabLayout.getTabAt(i).getCustomView();
            TextView tv = view.findViewById(R.id.tv_tab_item_textview);
            tv.setText(typeDatas.get(i).getProjectscale());
            tv.setTextSize(15);
            tv.setTextColor(getResources().getColor(R.color.white));
        }

        tabPosition = getArguments().getInt("tabPosition");
        if (tabPosition == -1) {
            tabPosition = 0;
        }
        binding.tabLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.tabLayout.getTabAt(tabPosition).select(); //默认选中某项放在加载viewpager之后
            }
        }, 5);

    }

    private void setMarkerBig(String title) {
        LatLng latLng = null;
        for (MarkerEntity markerEntity : markerEntityList) {
            if (markerEntity.getTitle().equals(title)) {
                markerEntity.setBig(true);
            } else {
                markerEntity.setBig(false);
            }
            if (markerEntity.isBig()) {
                if (markerEntity.getBusinessType() == 2) {
                    markerEntity.setIcon(R.mipmap.ic_map_overlay_jing_big);
                } else if (markerEntity.getBusinessType() == 1) {
                    markerEntity.setIcon(R.mipmap.ic_map_overlay_biao_big);
                } else if (markerEntity.getBusinessType() == 3) {
                    markerEntity.setIcon(R.mipmap.ic_map_overlay_yu_big);
                }
                latLng = markerEntity.getLatLng();
            } else {
                if (markerEntity.getBusinessType() == 2) {
                    markerEntity.setIcon(R.mipmap.ic_map_overlay_jing);
                } else if (markerEntity.getBusinessType() == 1) {
                    markerEntity.setIcon(R.mipmap.ic_map_overlay_biao);
                } else if (markerEntity.getBusinessType() == 3) {
                    markerEntity.setIcon(R.mipmap.ic_map_overlay_yu);
                }
            }
        }
        if (isShowPopWindow) {
            binding.mMapView.setMarkerDataPick(markerEntityList, latLng);
        } else {
            binding.mMapView.setMarkerDataPick(markerEntityList, latLngLocation);
        }
    }

    private void showMapPopWindow(int position) {
        isMarkerClick = true;
        switch (bidTypeNo) {
            case "0":
                if (pickMapPopAllWindow != null) {
                    if (!pickMapPopAllWindow.isShowing()) {
                        pickMapPopAllWindow.showDownPopwindow(binding.tvDistance, true);
                        pickMapPopAllWindow.backgroundAlpha(1f);
                    }
                    pickMapPopAllWindow.setData(tenderAllListEntityList, position);
                    pickMapPopAllWindow.setFocusable(false);
                    pickMapPopAllWindow.setOutsideTouchable(false);
                    pickMapPopAllWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
                    pickMapPopAllWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
                }
                break;
            case "1":
                if (pickMapPopWindow != null) {
                    if (!pickMapPopWindow.isShowing()) {
                        pickMapPopWindow.showDownPopwindow(binding.tvDistance, true);
                        pickMapPopWindow.backgroundAlpha(1f);
                    }
                    pickMapPopWindow.setData(tenderListEntityMapList, position);
                    pickMapPopWindow.setFocusable(false);
                    pickMapPopWindow.setOutsideTouchable(false);
                    pickMapPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
                    pickMapPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
                }
                break;
            case "2":
                if (pickMapBiddingPopWindow != null) {
                    if (!pickMapBiddingPopWindow.isShowing()) {
                        pickMapBiddingPopWindow.showDownPopwindow(binding.tvDistance, true);
                        pickMapBiddingPopWindow.backgroundAlpha(1f);
                    }
                    pickMapBiddingPopWindow.setData(biddingListEntityMapList, position);
                    pickMapBiddingPopWindow.setFocusable(false);
                    pickMapBiddingPopWindow.setOutsideTouchable(false);
                    pickMapBiddingPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
                    pickMapBiddingPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
                }
                break;
        }
    }

    public void showAllMapList(List<TenderAllListEntity> tenderAllListEntities, boolean lastPage, boolean b) {

        if (!b) {
            if (tenderAllListEntities == null || tenderAllListEntities.size() == 0) {
                binding.mMapView.updateMarkerDataPick(null, !b, latLngLocation);
                return;
            }
            markerEntityList.clear();
        }
        for (TenderAllListEntity entity : tenderAllListEntities) {
            if (!TextUtils.isEmpty(entity.getLatitude()) && !TextUtils.isEmpty(entity.getLongitude())) {
                LatLng latLng = new LatLng(Double.valueOf(entity.getLatitude()), Double.valueOf(entity.getLongitude()));
                MarkerEntity markerEntity = null;
                if (entity.getState() == 5) {  //废标
                    markerEntity = new MarkerEntity(latLng, R.mipmap.zhao_gray, entity.getId());
                } else {
                    if (entity.getBusinessType() == 2) {
                        markerEntity = new MarkerEntity(latLng, R.mipmap.ic_map_overlay_jing, entity.getId());
                    } else if (entity.getBusinessType() == 1) {
                        markerEntity = new MarkerEntity(latLng, R.mipmap.ic_map_overlay_biao, entity.getId());
                    } else if (entity.getBusinessType() == 3) {
                        markerEntity = new MarkerEntity(latLng, R.mipmap.ic_map_overlay_yu, entity.getId());
                    }
                }
                markerEntity.setBusinessType(entity.getBusinessType());
                markerEntity.setStatus(Integer.toString(entity.getState()));
                markerEntity.setBig(false);
                markerEntityList.add(markerEntity);
            }
        }

        binding.mMapView.updateMarkerDataPick(markerEntityList, !b, latLngLocation);
    }

    public void showTenderMapList(List<TenderListEntity> tenderListEntityList, boolean lastPage,
                                  boolean b) {
        if (!b) {
            if (tenderListEntityList == null || tenderListEntityList.size() == 0) {
                binding.mMapView.updateMarkerDataPick(null, !b, latLngLocation);
                return;
            }
            markerEntityList.clear();
        }
        for (TenderListEntity tenderListEntityMap : tenderListEntityList) {
            if (!TextUtils.isEmpty(tenderListEntityMap.getLocalLat()) && !TextUtils.isEmpty(tenderListEntityMap.getLocalLng())) {
                LatLng latLng = new LatLng(Double.valueOf(tenderListEntityMap.getLocalLat()), Double.valueOf(tenderListEntityMap.getLocalLng()));
                MarkerEntity markerEntity;
                if ("5".equals(tenderListEntityMap.getStatus())) {  //废标
                    markerEntity = new MarkerEntity(latLng, R.mipmap.zhao_gray, tenderListEntityMap.getId());
                } else {
                    markerEntity = new MarkerEntity(latLng, R.mipmap.ic_map_overlay_biao, tenderListEntityMap.getId());
                }
                markerEntity.setStatus(tenderListEntityMap.getStatus());
                markerEntityList.add(markerEntity);
            }
        }

        binding.mMapView.updateMarkerDataPick(markerEntityList, !b, latLngLocation);
    }

    public void showBidMapList(List<BiddingEntity> biddingEntityList, boolean b) {
        if (!b) {
            if (biddingEntityList == null || biddingEntityList.size() == 0) {
                binding.mMapView.updateMarkerDataPick(null, !b, latLngLocation);
                return;
            }
            markerEntityList.clear();
        }

        for (BiddingEntity entity : biddingEntityList) {
            if (!TextUtils.isEmpty(entity.getLocalLat()) && !TextUtils.isEmpty(entity.getLocalLng())) {
                LatLng latLng = new LatLng(Double.valueOf(entity.getLocalLat()), Double.valueOf(entity.getLocalLng()));
                MarkerEntity markerEntity;
                if (entity.getState() == 5) {  //废标
                    markerEntity = new MarkerEntity(latLng, R.mipmap.zhao_gray, entity.getId());
                } else {
                    markerEntity = new MarkerEntity(latLng, R.mipmap.ic_map_overlay_jing, entity.getId());
                }
                markerEntity.setStatus(Integer.toString(entity.getState()));
                markerEntityList.add(markerEntity);
            }
        }

        binding.mMapView.updateMarkerDataPick(markerEntityList, !b, latLngLocation);
    }

    private void showPositionMap(int position) {
        if (markerEntityList != null && markerEntityList.size() > position) {
            String title = markerEntityList.get(position).getTitle();
            if (title != null) {
                MarkerOptions markerOptions = binding.mMapView.getMapMarkerPick(title);
                if (markerOptions != null) {
                    setMarkerBig(markerOptions.getTitle());
                    if (isShowPopWindow && isMarkerClick) {
                        showMapPopWindow(position);
                    }
                }
            }
        }
    }

    public void setListEntityAllMapList(List<TenderAllListEntity> datas, boolean faage, int page) {
        if (datas == null || datas.size() == 0) {
            if (!faage) {
                mapBiddingPopWindowDismiss();
                binding.mMapView.updateMarkerDataPick(null, !faage, latLngLocation);
                if (isSearch) {
                    MToast.showToast(getContext(), "当前地址没有信息");
                } else {
                    MToast.showToast(getContext(), "没有更多数据了");
                }
            } else {
                MToast.showToast(getContext(), "没有更多数据了");
            }
            isSearch = false;
            return;
        }
        if (faage) {
            if (datas != null && datas.size() > 0) {
                if (tenderAllListEntityList.size() < page * 10) {
                    tenderAllListEntityList.addAll(datas);
                }
                showPositionMap((page - 1) * 10);
            }
        } else {
            tenderAllListEntityList.clear();
            tenderAllListEntityList.addAll(datas);
            showPositionMap(0);
        }
    }

    public void setListEntityTenderMapList(List<TenderListEntity> list, boolean b, int page) {
        if (list == null || list.size() == 0) {
            if (!b) {
                mapBiddingPopWindowDismiss();
                binding.mMapView.updateMarkerDataPick(null, !b, latLngLocation);
                if (isSearch) {
                    MToast.showToast(getContext(), "当前地址没有信息");
                } else {
                    MToast.showToast(getContext(), "没有更多数据了");
                }
            } else {
                MToast.showToast(getContext(), "没有更多数据了");
            }
            isSearch = false;
            return;
        }

        if (b) {
            if (list != null && list.size() > 0) {
                if (tenderListEntityMapList.size() < page * 10) {
                    tenderListEntityMapList.addAll(list);
                }
                showPositionMap((page - 1) * 10);
            }
        } else {
            tenderListEntityMapList.clear();
            tenderListEntityMapList.addAll(list);
            showPositionMap(0);
        }

    }

    public void setListEntityBidMapList(List<BiddingEntity> datas, boolean faage, int page) {
        if (datas == null || datas.size() == 0) {
            if (!faage) {
                mapBiddingPopWindowDismiss();
                binding.mMapView.updateMarkerDataPick(null, !faage, latLngLocation);
                if (isSearch) {
                    MToast.showToast(getContext(), "当前地址没有信息");
                } else {
                    MToast.showToast(getContext(), "没有更多数据了");
                }
            } else {
                MToast.showToast(getContext(), "没有更多数据了");
            }
            isSearch = false;
            return;
        }

        if (faage) {
            if (datas != null && datas.size() > 0) {
                if (biddingListEntityMapList.size() < page * 10) {
                    biddingListEntityMapList.addAll(datas);
                }
                showPositionMap((page - 1) * 10);
            }
        } else {
            biddingListEntityMapList.clear();
            biddingListEntityMapList.addAll(datas);
            showPositionMap(0);
        }
    }

    private void mapBiddingPopWindowDismiss() {
        isShowPopWindow = false;
        switch (bidTypeNo) {
            case "0":
                if (pickMapPopAllWindow != null && pickMapPopAllWindow.isShowing()) {
                    pickMapPopAllWindow.dismiss();
                }
                break;
            case "1":
                if (pickMapPopWindow != null && pickMapPopWindow.isShowing()) {
                    pickMapPopWindow.dismiss();
                }
                break;
            case "2":
                if (pickMapBiddingPopWindow != null && pickMapBiddingPopWindow.isShowing()) {
                    pickMapBiddingPopWindow.dismiss();
                }
                break;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK) {
            if (requestCode == 255) {
                if (data == null) {
                    setSearchContent(defaultAddress);
                    return;
                }

                Double latitudeHand = data.getDoubleExtra(SelectAddressActivity.LATITUDE, 0);
                Double longitudeHand = data.getDoubleExtra(SelectAddressActivity.LONGTITUDE, 0);
                String address = data.getStringExtra(SelectAddressActivity.CURRENT_POSITION);

                if (!"地址信息".equals(address)) {
                    setSearchContent(address);
                }

                if (latitude == null && longitude == null) {  //自动定位未获取到位置 第一次
                    if (latitudeHand != 0 || longitudeHand != 0) {  //手动定位获取到位置
                        latitude = latitudeHand;
                        longitude = longitudeHand;
                        binding.mMapView.setVisibility(View.VISIBLE);
                        binding.mMapView.setMyLocation(latitude, longitude, R.drawable.location);
                    }
                } else {
                    if (!latitude.equals(latitudeHand) || !longitude.equals(longitudeHand)) {
                        latitude = latitudeHand;
                        longitude = longitudeHand;
                        binding.mMapView.setMyLocation(latitude, longitude, R.drawable.location);
                    }
                }
                latLngLocation = new LatLng(Double.valueOf(latitude), Double.valueOf(longitude));
                isSearch = true;
                initFilterData(bidTypeNo);
            }
        }
    }

    //用户是否打开系统定位服务
    public boolean isLocationEnabled() {
        int locationMode = 0;
        String locationProviders;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                locationMode = Settings.Secure.getInt(getActivity().getContentResolver(), Settings.Secure.LOCATION_MODE);
            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
                return false;
            }
            return locationMode != Settings.Secure.LOCATION_MODE_OFF;
        } else {
            locationProviders = Settings.Secure.getString(getActivity().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            return !TextUtils.isEmpty(locationProviders);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void jumpLogin() {
        boolean shanyan = MkvUtils.get().getBoolean(MkvUtils.CommomData, CacheMarker.ShanYan, false);
        if (shanyan) {
            //校验权限
            //申请动态权限
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //6.0以上运行时权限处理
                EsayPermissions.with(getActivity()).constantRequest().permission(PERMISSIONS).request(new OnPermission() {
                    @Override
                    public void hasPermission(List<String> granted, boolean isAll) {
                        openLoginPage();
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {
                        openLoginPage();
                    }
                });

            } else {
                //有权限
                openLoginPage();
            }

        } else {
            VerificationCodeLoginActivity.start(getContext(), EventConstant.EVENT_TENDER_DETAILS_3_JUMP);
        }
    }

    //闪验
    static final String[] PERMISSIONS = new String[]{
            Manifest.permission.READ_PHONE_STATE
    };

    /**
     * 拉起授权页
     */
    private void openLoginPage() {
        //设置授权页样式
        OneKeyLoginManager.getInstance().setAuthThemeConfig(ConfigUtils.getCJSConfig(getContext()));
        //开始拉取授权页
        DialogHelper.showProgressDialog(getContext(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(true);
        OneKeyLoginManager.getInstance().openLoginAuth(false, new OpenLoginAuthListener() {
            @Override
            public void getOpenLoginAuthStatus(int code, String result) {
                DialogHelper.dismissProgressDialog();
                if (code != 1000) {
                    VerificationCodeLoginActivity.start(getContext(), EventConstant.EVENT_TENDER_DETAILS_3_JUMP);
                }
            }
        }, new OneKeyLoginListener() {
            @Override
            public void getOneKeyLoginStatus(int code, String result) {
                DialogHelper.dismissProgressDialog();
                if (code == 1000) {
                    OneKeyLoginEntity oneKeyLoginEntity = GsonManager.getInstance().getGson().fromJson(result, OneKeyLoginEntity.class);
                    oneKeyLoginEntity.setPlateform("Android");
                    pickMapFragmentModel.oneKeyLogin(oneKeyLoginEntity);
                }
            }
        });
    }

    /**
     * 一键登录成功
     *
     * @param loginEntity
     * @param phoneNumber
     */
    public void loginSucceed(LoginEntity loginEntity, String phoneNumber) {
        LoginEvent loginEvent = new LoginEvent();
        loginEvent.setMessageData(phoneNumber);
        loginEvent.setMessageTag(EventConstant.EVENT_TENDER_DETAILS_3_JUMP);
        loginEvent.setObject(loginEntity);
        EventBus.getDefault().postSticky(loginEvent);
    }

    public void getUserMsgSucceed(UserMsgEntity userMsgEntity) {
        LoginEvent loginEvent = new LoginEvent();
        loginEvent.setMessageTag(EventConstant.EVENT_USER_MSG);
        loginEvent.setMessageData(userMsgEntity);
        EventBus.getDefault().postSticky(loginEvent);
    }
}
